package com.microservices.yucloud.api.controller;

import com.microservices.yucloud.api.mongo.service.UserService;
import com.microservices.yucloud.api.mongo.entity.UserEntry;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author zy
 * @date 2019-08-20 19:04
 **/
@Slf4j
@RestController
@RequestMapping("mongo")
public class MongoController {

    @Autowired
    private UserService userService;

    @GetMapping("/test")
    public String add() {
        UserEntry userEntry = new UserEntry();
        userEntry.setId("2");
        userEntry.setName("mark");
        userEntry.setCreateById("1");
        userEntry.setLastModifyById("1");
        userEntry.setPassword("666");
        userService.save(userEntry);
        return "ok";
    }

    @GetMapping("/test1")
    public String update() {
        UserEntry userEntry = new UserEntry();
        userEntry.setId("2");
        userEntry.setName("tony");
        userEntry.setCreateById("1");
        userEntry.setLastModifyById("2");
        userEntry.setPassword("123");
        userService.update(userEntry);
        return "ok";
    }

    @GetMapping("/find")
    public Object findAll() {
        log.info("查询所有user");
        return userService.findAll();
    }

    @GetMapping("/find/{name}")
    public Object find(@PathVariable String name) {
        log.info("name:[{}]", name);
        return userService.findByName(name);
    }
}
